use std::io::Write;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    loop {
        let line = readline()?;
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        if !respond(line) {
            break;
        }
    }
    Ok(())
}

fn respond(line: &str) -> bool {
    match line {
        "exit" => false,
        _ => match calc::calc(line) {
            Ok(i) => {
                println!("=== {i}");
                true
            }
            Err(err) => {
                println!("=== {err}");
                true
            }
        },
    }
}

fn readline() -> Result<String, Box<dyn std::error::Error>> {
    write!(std::io::stdout(), ">>> ")?;
    std::io::stdout().flush()?;
    let mut buffer = String::new();
    std::io::stdin().read_line(&mut buffer)?;
    Ok(buffer)
}
